/**
 * Copyright (c) 2011 - 2012,  Inc. All rights reserved.
 * @fileoverview   表单--number
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-06-29
 * @param opt{
 * 	'el': '',       // input标签
 *  'require':['focus','blur']	//是否需要调用focus blur函数
 * }
 * form_data="isMust=1&isChange=1&minValue=0&maxValue=10000"
 * @example
 * var a = new STK.cpt.extend.formNum(opt);
 */
 
 
 
$require('form.cFormTips');
$require('form.formInputText')
LJS.define('form.cFormNum',function(ljs){

    var cFormTips = ljs.form.cFormTips;
    var formInputText = ljs.form.formInputText;
    var formNumTpl  =  {
		tip:'请输入数字',
		error:'非法数值'
	}
    
    
     function formNum(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        that.el = opt.el;
        that.require = opt.require||[];
        that.value = that.el.value||null;
        that.formNumTpl = opt.formNumTpl || formNumTpl;
        var formData = that.el.getAttribute('form_data');
        that.data = typeof formData === 'string' ? $.queryToJson(formData) : {};
        that.minValue = parseFloat(that.data.minValue) || '';
        that.maxValue = parseFloat(that.data.maxValue) || '';
       // 是否实时判断合法性
        that.changeStore = parseInt(that.data.isChange) || 0;
        that.isChange = 0;
		that.state = false;
        that.isMust = parseInt(that.data.isMust) || 0;
		that.posObj = opt.posObj;
        that.init();
    }
    formNum.prototype = new formInputText;
    formNum.prototype.constructor = formInputText
    formNum.prototype.init = function(){
        var that = this;
		that.value !== null ? that.setValue(that.value) : '';
		that.tip = new cFormTips({
            'el': that.el,
			'posObj' : that.posObj
        });
        $.foreach(that.require, function(a, i){
            switch (a) {
                case 'focus':
                    that.focus({
                        callback: function(){
                          that.state === true ? '' : that.showTip();
                        },
                        loop: function(){
                           that.check() ? that.hide() : that.showError();
                        }
                    });
                    break;
                case 'blur':
                    that.blur({
                        callback: function(){
							that.setValue(that.integerNum(that.getValue()));
                            if (that.check()) {
                                that.isChange = 0;
                                that.showRight();
                                that.state = false;
                            }
                            else {
                                that.showError();
								//$.addClassName(that.el, 'error');
                                that.isChange = that.changeStore;
                                that.state = true;
                            }
                        }
                        
                        
                    });
                    break;
                case 'select':
                    that.select({
                        callback: function(){
                        }
                    });
                    break;
                default:
                    return false;
            }
        });
    };
    /**
     * 检测number是否符合要求
     */
    formNum.prototype.check = function(){
        var that = this;
        var numValue = that.getValue();
        if (that.isMust) {
			if (that.isEmpty()) return false;
			if (!that.regNumber(numValue)) //当输入的不合法时
				return false;
			if (numValue=== 0 && numValue>=that.minValue) return true;
			
			if (that.minValue && that.maxValue) {
				return numValue <= that.maxValue && numValue >= that.minValue;
			}
			if (that.minValue) {
				return numValue >= that.minValue;
			}
			if (that.maxValue) {
				return numValue <= that.maxValue;
			}
			return true;
		}
		else {
			if (that.isEmpty()) 
				return true;
			if (numValue === 0 && numValue>=that.minValue) 
				return true;
			if (!numValue) //当输入的为字母是 Number（'abc'）==NaN
				return false;
			if (that.minValue && that.maxValue) {
				return numValue <= that.maxValue && numValue >= that.minValue;
			}
			if (that.minValue) {
				return numValue >= that.minValue;
			}
			if (that.maxValue) {
				return numValue <= that.maxValue;
			}
			return true;
		}
        
    };
	//验证是否为整数
	 formNum.prototype.regNumber = function(str){
	 	var r =/^\d+$/;//正整数+0
 		return  r.test(str);
	 }
	// 去掉整数前面多余的0         
	 formNum.prototype.integerNum = function(str){
	 	str = str.replace(/\s/g,'');
         if (str.replace(/^0+/,'')!='' && str !== '') 
             return str.replace(/^0+/,'')
         return str;
	 }
	// 显示提示层
    formNum.prototype.showTip = function(){
      var that = this;
      that.tip.show(that.formNumTpl.tip);
    };
    // 隐藏提示层
    formNum.prototype.hide = function(){
      var that = this;
	  $.removeClassName(that.el,'error');
      that.tip.hide();
	  
    };
    // 显示错误提示层
    formNum.prototype.showError = function(){
     var that = this;
     that.tip.show(that.formNumTpl.error,1);
	 $.addClassName(that.el,'error');
    };
    // showRight
    formNum.prototype.showRight = function(){
     var that = this;
     that.tip.show('',2);
	 $.addClassName(that.el,'error');
    };
    /**
     * 设置form_datad 值
     * 	J2Q();
     */
    formNum.prototype.J2Q = function(){
        var that = this;
        var opt = {
            'maxValue':  that.maxValue || '', 
            'minValue':  that.minValue || '',
            'isChange': that.isChange || 0,
            'isMust': that.isMust || 0
        }
        that.data = $.jsonToQuery(opt);
        that.el.setAttribute('form_data', that.data);
    };
    return formNum;
});
